
package com.chy.utilsdemo.Sm4Util;

import java.io.UnsupportedEncodingException;

/**
 * 功能： 国密SM4分组密码算法工具类示例代码
 *
 * ──────────────────────────────────────────
 *   version  变更日期    修改人    修改说明
 * ------------------------------------------
 *   V1.0.0   2021/9/16     CHY     初版
 * ──────────────────────────────────────────
 */
public class SM4UtilsTest {

    public static void main(String[] args) throws UnsupportedEncodingException {
        String plainText ="Hello world!";

        SM4Utils sm4 = new SM4Utils();
        sm4.setSecretKey("11HDESaAhiHHugDz");

        sm4.setIv("");
        plainText.getBytes("UTF-8");
        System.out.println("ECB模式");
        String cipherText = sm4.encryptData_ECB(plainText);
        System.out.println("ECB模式加密后: " + cipherText);

        plainText = sm4.decryptData_ECB(cipherText);
        System.out.println("ECB模式解密后: " + plainText);
        System.out.println("");

        System.out.println("CBC模式");
        sm4.setIv("UISwD9fW6cFh9SNS");
        cipherText = sm4.encryptData_CBC(plainText);
        System.out.println("CBC模式加密后: " + cipherText);

        plainText = sm4.decryptData_CBC(cipherText);
        System.out.println("CBC模式解密后: " + plainText);
    }
}
